/**
 * Created by ljw on 2017/4/26.
 */
var requestjson = require('request-json');
var customers = require('./customer.json');
var purchases = require('./purchase.json');

var client = requestjson.createClient('http://172.60.1.216:8899');
(async function(){
    for (let item of customers){
        await add('Customer',item)
    }
    console.log("添加客户成功",customers.length)

    for (let item of purchases){
        item = await findCustomerNum(item)
        await add('Purchase',item)
    }
    console.log("添加采购单成功",purchases.length)

})()

function findCustomerNum(item){
    return new Promise(function(resolv, reject) {
        client.post('Customer/fetch?token=595629335317a74d44b77c06', {filter:{'name':item.customer_num},startPos:0, limit:99999}, function(err, res, body) {
            item.customer_num = body.data.list[0].cnum;
            resolv(item);
        });
    })
}

function add(model, item){
    return new Promise(function(resolv, reject) {
        client.post(model+'/addItem?token=595629335317a74d44b77c06', {item:item}, function(err, res, body) {
            resolv();
        });
    })
}
